package com.my.controller;


import com.my.pojo.UserInfo;
import com.my.service.IUserInfoService;
import com.my.service.impl.UserInfoServiceImpl;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/user/userInfoController")
public class UserInfoController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        PrintWriter out=res.getWriter();
        // res.setCharacterEncoding("utf-8");
        String result="";
        HttpSession session=req.getSession();
        String userName=req.getParameter("userName");
        String password=req.getParameter("password");
        String address=req.getParameter("address");
        System.out.println(userName+","+password);
        ServletContext sc = this.getServletContext();
        WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(sc);
        IUserInfoService userInfoService=wac.getBean("userInfoService", UserInfoServiceImpl.class);
        Object obj=userInfoService.isExistFileInfo(userName,password);
        System.out.println("o="+obj);
        if(obj==null){
            UserInfo userInfo=new UserInfo();
            userInfo.setUserName(userName);
            userInfo.setPassword(password);
            userInfo.setAddress(address);
            userInfoService.insertUserInfo(userInfo);
            session.setAttribute("userName",userName);
            result="success.jsp";
        }else{
           result="error.jsp";
        }
        res.sendRedirect(result);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            this.doGet(req,resp);
    }
}
